package cn.com.agree.bigdata.vmdatahandle.config;

import lombok.extern.slf4j.Slf4j;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;


/**
 * @description: config parser.
 * @author: NiYanchun niyanchun@agree.com.cn
 * @version: 1.0
 * @create: 2019-01-08 17:49
 **/
@Slf4j
public class YamlParser {
    private static AppConfig appConfig;

    public static AppConfig getAppConfig(String configFile) {
        // if appConfig is not null, indicate it has been parsed, just return.
        if (appConfig != null) {
            return appConfig;
        }

        Yaml yaml = new Yaml(new Constructor(AppConfig.class));
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(configFile);
        } catch (FileNotFoundException e) {
            log.error("config file {} not found!", configFile);
            e.printStackTrace();
            System.exit(1);
        }

        appConfig = yaml.load(inputStream);
        return appConfig;
    }
}
